/*
 * Created by JFormDesigner on Fri Jun 28 14:31:45 CST 2024
 */

package scct.chenlixian.ui;

import scct.chenlixian.dao.TypeDao;
import scct.chenlixian.model.GoodsType;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.util.List;

/**
 * @author DELL
 */
public class PanelType extends JPanel {
    DefaultTableModel model;
    FormMain main;

    public PanelType(FormMain formMain) {
        this.main = formMain;
        initComponents();
        initView();
    }

    private void initView() {
       model = (DefaultTableModel) tabType.getModel();
        model.addColumn("id");
        model.addColumn("类别名称");
        model.addColumn("类别说明");
        getTable();

    }

    private void getTable() {
        model.setRowCount(0);
        TypeDao typeDao = new TypeDao();
        List<GoodsType> allTyps = typeDao.getAllTyps();
        for (GoodsType allTyp : allTyps) {
            model.addRow(new Object[]{
                    allTyp.getId(),
                    allTyp.getName(),
                    allTyp.getRemark(),
            });
        }
    }

    private void btnADD(ActionEvent e) {
        DlgADDType dlgADDType = new DlgADDType(main);
        dlgADDType.setModal(true);
        dlgADDType.setVisible(true);
        getTable();
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        // Generated using JFormDesigner Evaluation license - CLX
        panel1 = new JPanel();
        btnADD = new JButton();
        btnUpdate = new JButton();
        scrollPane1 = new JScrollPane();
        tabType = new JTable();

        //======== this ========
        setBorder(new javax.swing.border.CompoundBorder(new javax.swing.border.TitledBorder(new javax.swing
                .border.EmptyBorder(0, 0, 0, 0), "JF\u006frmDesi\u0067ner Ev\u0061luatio\u006e", javax.swing.border.TitledBorder
                .CENTER, javax.swing.border.TitledBorder.BOTTOM, new java.awt.Font("Dialo\u0067", java.
                awt.Font.BOLD, 12), java.awt.Color.red), getBorder()))
        ;
        addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            @Override
            public void propertyChange(java.beans.PropertyChangeEvent e
            ) {
                if ("borde\u0072".equals(e.getPropertyName())) throw new RuntimeException();
            }
        })
        ;
        setLayout(new BorderLayout());

        //======== panel1 ========
        {
            panel1.setLayout(new FlowLayout(FlowLayout.RIGHT));

            //---- btnADD ----
            btnADD.setText("\u65b0\u7c7b\u522b\u589e");
            btnADD.addActionListener(e -> btnADD(e));
            panel1.add(btnADD);

            //---- btnUpdate ----
            btnUpdate.setText("\u4fee\u6539\u7c7b\u522b");
            panel1.add(btnUpdate);
        }
        add(panel1, BorderLayout.NORTH);

        //======== scrollPane1 ========
        {
            scrollPane1.setViewportView(tabType);
        }
        add(scrollPane1, BorderLayout.CENTER);
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    // Generated using JFormDesigner Evaluation license - CLX
    private JPanel panel1;
    private JButton btnADD;
    private JButton btnUpdate;
    private JScrollPane scrollPane1;
    private JTable tabType;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
